﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media;
using Signage01.rectangles;
using System.Windows;
using System.Windows.Input;
using System.Windows.Shapes;

namespace Signage01
{
    class Drawing
    {
        private MainWindow mainWindow;
        private Canvas canvas;

        private List<RectangleDraw> listRectangleDraw;
        private List<ScreenDivision> listScreenDivisionDraw;

        private int _countRectangle;
        public List<Color> colorsRect;

        // code for canvas of screen div
        private int zOrder = 0;
        private int initialX = 20;
        private int initialY = 20;
        private int indexColor = 0;
        //
        // variables for rectange move in canvas
        bool captured = false;
        UIElement source = null;

        double initialMoveX;
        double initialMoveY;

        double finalMoveX;
        double finalMoveY;
        //
        // variables for rectangle resize
        enum Sides { Up, Down, Left, Right, None };
        //ScreenDivision screenDivResizeSelected = null;
        Sides selectedSideResize;
        bool flagRectangleResize = false;

        double initialResizeX;
        double initialResizeY;

        double finalResizeX;
        double finalResizeY;

        //
        //private ScreenDivision screenDivisionSelected = null;
        private int borderCursorWidth = 1;
        private int _lastRectangleNo;

        public int LastRectangleNo
        {
            get { return _lastRectangleNo; }
            set { _lastRectangleNo = value; }
        }

        public int CountRectangle
        {
            get
            {
                return _countRectangle;
            }

            set
            {
                _countRectangle = value;
            }
        }

        public void incrementCountRectangle()
        {
            this.CountRectangle++;
        }

        // this function is needed if screen template loaded from file
        /*
        public void setRectangleDraw(ScreenTemplate screenTemplateSelected) 
        {
            foreach (ScreenDivision tempScreenDivision in screenTemplateSelected.ListScreenDivision)
            {
                RectangleDraw rectangleDraw = getRectangleDrawFromNumber(tempScreenDivision.RectangleNumber);
                listRectangleDraw.Add(rectangleDraw);
                incrementCountRectangle();
            }
        }
        */

        
        public void initializeAllRectangles(List<ScreenTemplate> listScreenTemplate)
        {
            foreach (ScreenTemplate screenTemplate in listScreenTemplate)
            {
                foreach (ScreenDivision sd in screenTemplate.ListScreenDivision)
                {                    
                    listRectangleDraw.Add(getRectangleFromScreenDiv(sd));
                    incrementCountRectangle();
                    if(LastRectangleNo<sd.RectangleNumber)
                        LastRectangleNo = sd.RectangleNumber;
                }
            }
            Console.WriteLine("rectangle draw size:" + listRectangleDraw.Count());
        }
        // end of initializeAllRectangles method

        private RectangleDraw getRectangleFromScreenDiv(ScreenDivision sd)
        {
            RectangleDraw rectangleDraw = new RectangleDraw();
            rectangleDraw.Number = sd.RectangleNumber;

            Canvas.SetLeft(rectangleDraw.Rectangle, sd.RectangleDiv.X);
            Canvas.SetTop(rectangleDraw.Rectangle, sd.RectangleDiv.Y);

            rectangleDraw.Rectangle.Width = sd.RectangleDiv.Width;
            rectangleDraw.Rectangle.Height = sd.RectangleDiv.Height;

            Canvas.SetZIndex(rectangleDraw.Rectangle, sd.RectangleDiv.ZIndex);
            return rectangleDraw;

        }
        // end of getRectangleFromScreenDiv method


        public void addRectanglesForScreenTemplate()
        {

        }


        public Drawing(MainWindow mainWindow)
        {
            this.mainWindow = mainWindow;
            this.canvas = this.mainWindow.canvas;

            listRectangleDraw = new List<RectangleDraw>();
            listScreenDivisionDraw = new List<ScreenDivision>();

            _countRectangle = 0;

            initializeColorsRect();
            addEventsToCanvas();

            //initializeRectangleDraw();
        }
        // end of constructor method


        private void initializeColorsRect()
        {
            colorsRect = new List<Color>();
            colorsRect.Add(Colors.Red);
            colorsRect.Add(Colors.LightGreen);
            colorsRect.Add(Colors.Blue);
            colorsRect.Add(Colors.Magenta);
            colorsRect.Add(Colors.Chocolate);
            colorsRect.Add(Colors.Aquamarine);
            colorsRect.Add(Colors.Ivory);
            colorsRect.Add(Colors.Gray);
        }
        // end of initializeColorsRect method


        private void addEventsToCanvas()
        {
            canvas.AddHandler(Canvas.MouseLeftButtonDownEvent, new MouseButtonEventHandler(canvas_MouseLeftButtonDown_1));
            canvas.AddHandler(Canvas.MouseLeftButtonUpEvent, new MouseButtonEventHandler(canvas_MouseLeftButtonUp_1));
            canvas.AddHandler(Canvas.MouseMoveEvent, new MouseEventHandler(canvas_MouseMove_1));
        }
        // end of addEventsToCanvas method



        private void canvas_MouseLeftButtonUp_1(object sender, MouseButtonEventArgs e)
        {
            captured = false;
            Mouse.Capture(null);
            mainWindow.Cursor = Cursors.Arrow;            
            flagRectangleResize = false;
            mainWindow.screenDivResizeSelected = null;
            Console.WriteLine("");
           
        }


        private void canvas_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
        {
            source = (UIElement)sender;
            Mouse.Capture(source);
            captured = true;

            if (mainWindow.screenDivResizeSelected == null)
            {
                screenDivSelectEvent(e);
            }

            if (mainWindow.screenDivisionSelected != null) // mouse pressed on one of the rectangles
            {
                Rectangle rectangle = getRectangleDrawFromNumber(mainWindow.screenDivisionSelected.RectangleNumber).Rectangle;
                initialMoveX = Canvas.GetLeft(rectangle);
                initialMoveY = Canvas.GetTop(rectangle);

                //Console.WriteLine("initial x: " + initialX + ",y:" + initialY);

                finalMoveX = e.GetPosition(canvas).X;
                finalMoveY = e.GetPosition(canvas).Y;
                mainWindow.Cursor = Cursors.Hand;
            }

            if (mainWindow.screenDivResizeSelected != null) // mouse on the edge of one of the rectangles
            {
                Rectangle rectangle = getRectangleDrawFromNumber(mainWindow.screenDivResizeSelected.RectangleNumber).Rectangle;

                initialResizeX = Canvas.GetLeft(rectangle);
                initialResizeY = Canvas.GetTop(rectangle);

                //Console.WriteLine("initial x: " + initialX + ",y:" + initialY);

                finalResizeX = e.GetPosition(canvas).X;
                finalResizeY = e.GetPosition(canvas).Y;

                flagRectangleResize = true;

                if (selectedSideResize == Sides.Up || selectedSideResize == Sides.Down)
                {
                    mainWindow.Cursor = Cursors.SizeNS;
                }
                else if (selectedSideResize == Sides.Left || selectedSideResize == Sides.Right)
                {
                    mainWindow.Cursor = Cursors.SizeWE;
                }

                Console.WriteLine("Flag rectangle size enabled");
            }

        }
        // end of canvas_MouseLeftButtonDown_1 method

        private void unselectAllScreenDiv()
        {            

            if (mainWindow.screenTemplateSelected != null && mainWindow.screenTemplateSelected.ListScreenDivision.Count != 0)
            {
                List<ScreenDivision> listScreenDivision = mainWindow.screenTemplateSelected.ListScreenDivision;
                for (int i = 0; i < listScreenDivision.Count; i++)
                {
                    //Rectangle rect = listScreenDivision[i].Rectangle;
                    RectangleDraw rectDraw = getRectangleDrawFromNumber(listScreenDivision[i].RectangleNumber);
                    rectDraw.Rectangle.Stroke = new SolidColorBrush(colorsRect[i]);
                    rectDraw.Rectangle.StrokeThickness = 2;
                }
            }
        }


        private void screenDivSelectEvent(MouseButtonEventArgs e)
        {
            List<ScreenDivision> listScreenDivision = mainWindow.screenTemplateSelected.ListScreenDivision;

            unselectAllScreenDiv();

            canvas.UpdateLayout();
            //

            Point p = e.GetPosition(canvas);

            int maxZOrder = -1;
            // indexScreenDivision = -1;

            foreach (ScreenDivision sd in listScreenDivision)
            {
                /*
                int leftX = sd.StartX;
                int rightX = sd.StartX + sd.Width;
                int topY = sd.StartY;
                int bottomY = sd.StartY + sd.Height;
                */
                Rectangle rectangle = getRectangleDrawFromNumber(sd.RectangleNumber).Rectangle;

                double leftX = Canvas.GetLeft(rectangle);
                double rightX = Canvas.GetLeft(rectangle) + rectangle.Width;
                double topY = Canvas.GetTop(rectangle);
                double bottomY = Canvas.GetTop(rectangle) + rectangle.Height;

                if (p.X >= leftX && p.X <= rightX && p.Y >= topY && p.Y <= bottomY) // mouse clicked inside the screen div
                {
                    //listTempScreenDivision.Add(sd);
                    int zOrder = Canvas.GetZIndex(rectangle);
 
                    if (maxZOrder < zOrder)
                    {
                        maxZOrder = zOrder;
                        this.mainWindow.screenDivisionSelected = sd;
                    }
                    Console.WriteLine("Screen Div selected");
                }
            }

            if (mainWindow.screenDivisionSelected != null) // we get a screen division based on mouse click
            {
                Console.WriteLine("got it");
                Rectangle rect = getRectangleDrawFromNumber(mainWindow.screenDivisionSelected.RectangleNumber).Rectangle;
                rect.Stroke = new SolidColorBrush(Colors.Black);
                canvas.UpdateLayout();
                this.mainWindow.updateScreenDivProperties(mainWindow.screenDivisionSelected);
                mainWindow.gridScreenDivisionProperty.Visibility = Visibility.Visible;
                mainWindow.gridScreenProperty.Visibility = Visibility.Collapsed;
                mainWindow.gridScreenTemplateProperty.Visibility = Visibility.Collapsed;
            }
            else
            {
                Console.WriteLine("Out of rectangles");
            }
            //screenDivisionSelected = null;
            if (mainWindow.screenDivisionSelected == null)
            {
                Console.WriteLine("my god, it is null");
            }
            else
            {
                Console.WriteLine("no, not null");
            }
        }
        // end of screenDivSelectEvent method

        public void selectScreenDivProgramatically(ScreenDivision screenDivision)
        {
            try
            {
                unselectAllScreenDiv();
                Rectangle rect = getRectangleDrawFromNumber(screenDivision.RectangleNumber).Rectangle;
                rect.Stroke = new SolidColorBrush(Colors.Black);
                canvas.UpdateLayout();
                //this.mainWindow.updateScreenDivProperties(mainWindow.screenDivisionSelected);

                mainWindow.gridScreenDivisionProperty.Visibility = Visibility.Visible;
                mainWindow.gridScreenProperty.Visibility = Visibility.Collapsed;
                mainWindow.gridScreenTemplateProperty.Visibility = Visibility.Collapsed;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }


        public RectangleDraw getRectangleDrawFromNumber(int number)
        {
            foreach (RectangleDraw rectangleDraw in listRectangleDraw)
            {
                if (rectangleDraw.Number == number)
                {
                    return rectangleDraw;
                }
            }
            return null;
        }



        public void addRectangleDrawForScreenDiv(List<ScreenDivision> listScreenDivision, int currentIndexScreenDiv)
        {
            canvas.Children.Clear();
            listScreenDivisionDraw = listScreenDivision;

            int startX = (currentIndexScreenDiv + 1)*20;
            int startY = (currentIndexScreenDiv + 1)*20;
            /*
            screenDivision.Width= 200;
            screenDivision.Height = 100;
            screenDivision.ZOrder = currentIndexScreenDiv;
            */
            RectangleDraw rectangleDraw = new RectangleDraw();
            
            rectangleDraw.Number = LastRectangleNo ;

            Canvas.SetLeft(rectangleDraw.Rectangle, startX);
            Canvas.SetTop(rectangleDraw.Rectangle, startY);

            rectangleDraw.Rectangle.Width = 200;
            rectangleDraw.Rectangle.Height = 100;
            
            Canvas.SetZIndex(rectangleDraw.Rectangle, listScreenDivision.Count());

            listRectangleDraw.Add(rectangleDraw);

            incrementCountRectangle();
            draw();

        }

        public void rectangleDrawForScreenDiv(List<ScreenDivision> listScreenDivision)
        {
            listScreenDivisionDraw = listScreenDivision;
            //Console.WriteLine("screen div size:" + listScreenDivisionDraw.Count());
            draw();
        }


        public void draw()
        {
            canvas.Children.Clear();
            int indexColor = 0;

            Console.WriteLine("rectangle draw size:" + listRectangleDraw.Count());

            foreach (RectangleDraw rectangleDraw in listRectangleDraw)
            {
                foreach (ScreenDivision screenDivision in listScreenDivisionDraw)
                {
                    if (rectangleDraw.Number == screenDivision.RectangleNumber)
                    {
                        rectangleDraw.Rectangle.Stroke = new SolidColorBrush(colorsRect[indexColor]);
                        rectangleDraw.Rectangle.Fill = new SolidColorBrush(colorsRect[indexColor]);
                        rectangleDraw.Rectangle.StrokeThickness = 2;
                        canvas.Children.Add(rectangleDraw.Rectangle);
                        indexColor++;
                    }
                }
            }

            canvas.UpdateLayout();

        }


        private void canvas_MouseMove_1(object sender, MouseEventArgs e)
        {
            if (captured == true && mainWindow.screenDivisionSelected != null)
            {
                double finalNewMoveX = e.GetPosition(canvas).X;
                double finalNewMoveY = e.GetPosition(canvas).Y;

                double newX = initialMoveX + (finalNewMoveX - finalMoveX);
                double newY = initialMoveY + (finalNewMoveY - finalMoveY);

                Rectangle rectangle = getRectangleDrawFromNumber(this.mainWindow.screenDivisionSelected.RectangleNumber).Rectangle;

                Canvas.SetLeft(rectangle, newX);
                Canvas.SetTop(rectangle, newY);

                canvas.UpdateLayout();
                //y_canvas = y;

                initialMoveX = newX;
                initialMoveY = newY;

                finalMoveX = finalNewMoveX;
                finalMoveY = finalNewMoveY;

                Canvas.SetLeft(rectangle, Convert.ToDouble(initialMoveX));
                //mainWindow.screenDivisionSelected.StartX = Convert.ToInt32(initialMoveX);
                //screenDivisionSelected.StartY = Convert.ToInt32(initialMoveY);
                Canvas.SetTop(rectangle, Convert.ToDouble(initialMoveY));

                // screendiv rectangle updated
                this.mainWindow.screenDivisionSelected.RectangleDiv.X = Convert.ToDouble(initialMoveX);
                this.mainWindow.screenDivisionSelected.RectangleDiv.Y = Convert.ToDouble(initialMoveY);
                //
                mainWindow.updateScreenDivProperties(mainWindow.screenDivisionSelected);
            }

            if (flagRectangleResize == false)
            {
                if (checkIfMouseOnRectangleEdge(e))
                {
                    if (selectedSideResize == Sides.Up)
                    {
                        mainWindow.Cursor = Cursors.SizeNS;
                        Console.WriteLine("This is up again");
                        //flagRectangleResize = true;
                    }
                    else if (selectedSideResize == Sides.Left)
                    {
                        mainWindow.Cursor = Cursors.SizeWE;
                        Console.WriteLine("This is left again");
                        //flagRectangleResize = true;
                    }
                    else if (selectedSideResize == Sides.Down)
                    {
                        mainWindow.Cursor = Cursors.SizeNS;
                        Console.WriteLine("This is down again");
                        //flagRectangleResize = true;
                    }
                    else
                    {
                        mainWindow.Cursor = Cursors.SizeWE;
                        Console.WriteLine("This is right again");
                        //flagRectangleResize = true;
                    }
                }
                else
                {
                    mainWindow.Cursor = Cursors.Arrow;
                }
            }

            //if (flagRectangleResize==true && captured == true)
            if (flagRectangleResize == true)
            {
                if (selectedSideResize == Sides.Up)
                {
                    double finalNewResizeX = e.GetPosition(canvas).X;
                    double finalNewResizeY = e.GetPosition(canvas).Y;

                    double newX = initialResizeX;
                    double newY = finalNewResizeY;

                    Console.WriteLine("Resize new x: " + newX);

                    mainWindow.Cursor = Cursors.SizeNS;

                    //screenDivResizeSelected.StartX = Convert.ToInt32(newX);
                    //screenDivResizeSelected.StartY = Convert.ToInt32(newY);

                    Rectangle rectangle = getRectangleDrawFromNumber(mainWindow.screenDivResizeSelected.RectangleNumber).Rectangle;

                    Canvas.SetLeft(rectangle, newX);
                    Canvas.SetTop(rectangle, newY);
                    rectangle.Height = initialResizeY - finalNewResizeY + rectangle.Height;
                    //screenDivResizeSelected.Height = Convert.ToInt32(screenDivResizeSelected.Rectangle.Height);

                    // rectangle screendiv updated
                    this.mainWindow.screenDivResizeSelected.RectangleDiv.X = newX;
                    this.mainWindow.screenDivResizeSelected.RectangleDiv.Y = newY;
                    this.mainWindow.screenDivResizeSelected.RectangleDiv.Height = rectangle.Height;
                    //
                    //Console.WriteLine("New x:" + newX + ",y:" + newY + ",height:" + screenDivResizeSelected.Rectangle.Height);

                    canvas.UpdateLayout();
                    //y_canvas = y;

                    initialResizeX = newX;
                    initialResizeY = newY;

                    finalResizeX = finalNewResizeX;
                    finalResizeY = finalNewResizeY;

                    //screenDivResizeSelected = null;

                    mainWindow.updateScreenDivProperties(mainWindow.screenDivResizeSelected);
                }

                else if (selectedSideResize == Sides.Left)
                {
                    double finalNewResizeX = e.GetPosition(canvas).X;
                    double finalNewResizeY = e.GetPosition(canvas).Y;

                    double newX = finalNewResizeX;
                    double newY = initialResizeY;

                    Console.WriteLine("Resize new x: " + newX);

                    mainWindow.Cursor = Cursors.SizeWE;

                    //screenDivResizeSelected.StartX = Convert.ToInt32(newX);
                    //screenDivResizeSelected.StartY = Convert.ToInt32(newY);


                    Rectangle rectangle = getRectangleDrawFromNumber(mainWindow.screenDivResizeSelected.RectangleNumber).Rectangle;

                    Canvas.SetLeft(rectangle, newX);
                    Canvas.SetTop(rectangle, newY);
                    rectangle.Width = initialResizeX - finalNewResizeX + rectangle.Width;
                    //screenDivResizeSelected.Width = Convert.ToInt32(screenDivResizeSelected.Rectangle.Width);

                    // rectangle screendiv updated
                    this.mainWindow.screenDivResizeSelected.RectangleDiv.X = newX;
                    this.mainWindow.screenDivResizeSelected.RectangleDiv.Y = newY;
                    this.mainWindow.screenDivResizeSelected.RectangleDiv.Width = rectangle.Width;
                    //


                    Console.WriteLine("New x:" + newX + ",y:" + newY + ",height:" + rectangle.Height);

                    canvas.UpdateLayout();
                    //y_canvas = y;

                    initialResizeX = newX;
                    initialResizeY = newY;

                    finalResizeX = finalNewResizeX;
                    finalResizeY = finalNewResizeY;

                    //screenDivResizeSelected = null;
                    mainWindow.updateScreenDivProperties(mainWindow.screenDivResizeSelected);
                }

                else if (selectedSideResize == Sides.Down)
                {
                    double finalNewResizeX = e.GetPosition(canvas).X;
                    double finalNewResizeY = e.GetPosition(canvas).Y;

                    double newX = initialResizeX;
                    double newY = initialResizeY;

                    Console.WriteLine("Resize new x: " + newX);

                    mainWindow.Cursor = Cursors.SizeNS;

                    //screenDivResizeSelected.StartX = Convert.ToInt32(newX);
                    //screenDivResizeSelected.StartY = Convert.ToInt32(newY);

                    Rectangle rectangle = getRectangleDrawFromNumber(mainWindow.screenDivResizeSelected.RectangleNumber).Rectangle;

                    Canvas.SetLeft(rectangle, newX);
                    Canvas.SetTop(rectangle, newY);
                    rectangle.Height = finalNewResizeY - initialResizeY;
                    //screenDivResizeSelected.Height = Convert.ToInt32(screenDivResizeSelected.Rectangle.Height);

                    //Console.WriteLine("New x:" + newX + ",y:" + newY + ",height:" + screenDivResizeSelected.Rectangle.Height);
                    // rectangle screendiv updated
                    this.mainWindow.screenDivResizeSelected.RectangleDiv.X = newX;
                    this.mainWindow.screenDivResizeSelected.RectangleDiv.Y = newY;
                    this.mainWindow.screenDivResizeSelected.RectangleDiv.Height = rectangle.Height;
                    //

                    canvas.UpdateLayout();
                    //y_canvas = y;

                    initialResizeX = newX;
                    initialResizeY = newY;

                    finalResizeX = finalNewResizeX;
                    finalResizeY = finalNewResizeY;

                    //screenDivResizeSelected = null;

                    mainWindow.updateScreenDivProperties(mainWindow.screenDivResizeSelected);
                }

                else if (selectedSideResize == Sides.Right)
                {
                    double finalNewResizeX = e.GetPosition(canvas).X;
                    double finalNewResizeY = e.GetPosition(canvas).Y;

                    double newX = initialResizeX;
                    double newY = initialResizeY;

                    Console.WriteLine("Resize new x: " + newX);

                    mainWindow.Cursor = Cursors.SizeWE;

                    Console.WriteLine("Resize rect no: " + mainWindow.screenDivResizeSelected.RectangleNumber);
                    Rectangle rectangle = getRectangleDrawFromNumber(mainWindow.screenDivResizeSelected.RectangleNumber).Rectangle;
                    //screenDivResizeSelected.StartX = Convert.ToInt32(newX);
                    //screenDivResizeSelected.StartY = Convert.ToInt32(newY);


                    Canvas.SetLeft(rectangle, newX);
                    Canvas.SetTop(rectangle, newY);
                    rectangle.Width = finalNewResizeX - initialResizeX;
                    //screenDivResizeSelected.Width = Convert.ToInt32(screenDivResizeSelected.Rectangle.Width);

                    // rectangle screendiv updated
                    this.mainWindow.screenDivResizeSelected.RectangleDiv.X = newX;
                    this.mainWindow.screenDivResizeSelected.RectangleDiv.Y = newY;
                    this.mainWindow.screenDivResizeSelected.RectangleDiv.Width = rectangle.Width;
                    //


                    //Console.WriteLine("New x:" + newX + ",y:" + newY + ",height:" + screenDivResizeSelected.Rectangle.Height);

                    canvas.UpdateLayout();
                    //y_canvas = y;

                    initialResizeX = newX;
                    initialResizeY = newY;

                    finalResizeX = finalNewResizeX;
                    finalResizeY = finalNewResizeY;

                    mainWindow.updateScreenDivProperties(mainWindow.screenDivResizeSelected);

                }

            }
        }

        private bool checkIfMouseOnRectangleEdge(MouseEventArgs e)
        {
            if (this.mainWindow.screenTemplateSelected != null)
            {
                List<ScreenDivision> listScreenDivision = this.mainWindow.screenTemplateSelected.ListScreenDivision;

                double clickX = e.GetPosition(canvas).X;
                double clickY = e.GetPosition(canvas).Y;

                mainWindow.screenDivResizeSelected = null;

                foreach (ScreenDivision screenDivision in listScreenDivision)
                {
                    Rectangle rectangle = getRectangleDrawFromNumber(screenDivision.RectangleNumber).Rectangle;
                    double startX = Canvas.GetLeft(rectangle);
                    double startY = Canvas.GetTop(rectangle);

                    if (clickX >= startX - borderCursorWidth && clickX <= startX + rectangle.Width + borderCursorWidth && clickY >= startY - borderCursorWidth && clickY <= startY + borderCursorWidth)
                    {
                        mainWindow.screenDivResizeSelected = screenDivision;
                        selectedSideResize = Sides.Up;
                    }
                    else if (clickX >= startX - borderCursorWidth && clickX <= startX + borderCursorWidth && clickY >= startY - borderCursorWidth && clickY <= startY + rectangle.Height + borderCursorWidth)
                    {
                        mainWindow.screenDivResizeSelected = screenDivision;
                        selectedSideResize = Sides.Left;
                    }
                    else if (clickX >= startX - borderCursorWidth && clickX <= startX + rectangle.Width + borderCursorWidth && clickY >= startY + rectangle.Height - borderCursorWidth && clickY <= startY + rectangle.Height + borderCursorWidth)
                    {
                        mainWindow.screenDivResizeSelected = screenDivision;
                        selectedSideResize = Sides.Down;
                    }
                    else if (clickX >= startX + rectangle.Width - borderCursorWidth && clickX <= startX + rectangle.Width + borderCursorWidth && clickY >= startY - borderCursorWidth && clickY <= startY + rectangle.Height + borderCursorWidth)
                    {
                        mainWindow.screenDivResizeSelected = screenDivision;
                        selectedSideResize = Sides.Right;
                    }

                }

                if (mainWindow.screenDivResizeSelected == null)
                {
                    selectedSideResize = Sides.None;
                    //Console.WriteLine("This is nothing");
                }

                // now checking if the clicked point is overlapped by other rectangle with higher zorder

                if (mainWindow.screenDivResizeSelected != null)
                {
                    foreach (ScreenDivision screenDivision in listScreenDivision)
                    {
                        int zOrderScreenDivision = Canvas.GetZIndex(getRectangleDrawFromNumber(screenDivision.RectangleNumber).Rectangle);
                        int zOrderscreenDivResizeSelected = Canvas.GetZIndex(getRectangleDrawFromNumber(mainWindow.screenDivResizeSelected.RectangleNumber).Rectangle);

                        if (clickedWithinScreenDiv(clickX, clickY, screenDivision))
                        {
                            if (zOrderScreenDivision > zOrderscreenDivResizeSelected)
                            {
                                return false;
                            }
                        }

                    }

                    return true;
                }
                else
                {
                    return false;
                }
            }

            return false;

        }

        private bool clickedWithinScreenDiv(double clickX, double clickY, ScreenDivision screenDivision)
        {
            Rectangle rectangle = getRectangleDrawFromNumber(screenDivision.RectangleNumber).Rectangle;

            double startX = Canvas.GetLeft(rectangle);
            double startY = Canvas.GetTop(rectangle);

            if (clickX >= startX - borderCursorWidth && clickX <= startX + rectangle.Width + borderCursorWidth && clickY >= startY - borderCursorWidth && clickY <= startY + rectangle.Height + borderCursorWidth)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        
        public Canvas getCanvasFromScreenTemplate(ScreenTemplate screenTemplate)
        {
            Canvas canvas = new Canvas();
            canvas.Background = Brushes.Yellow;
            canvas.Width = screenTemplate.ScreenObject.Width;
            canvas.Height = screenTemplate.ScreenObject.Height;

            int indexTemp = 0;
            foreach (ScreenDivision screenDivision in screenTemplate.ListScreenDivision)
            {
                Rectangle rectangle = getRectangleFromScreenDiv(screenDivision).Rectangle;
                rectangle.Stroke = new SolidColorBrush(Colors.Blue);
                rectangle.StrokeThickness = 2;
                rectangle.Stroke = new SolidColorBrush(colorsRect[indexTemp]);
                rectangle.Fill = new SolidColorBrush(colorsRect[indexTemp]);
                canvas.Children.Add(rectangle);
                indexTemp++;
            }
            canvas.UpdateLayout();
            
            return canvas;
        }
        

    }
}
